﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using System.Collections.ObjectModel;
using CommunitySystem.JinShengService;
using CommunitySystem.PageView.晋升管理.新的晋升管理;
using EHR.Common;

namespace CommunitySystem.PageView
{
    public partial class KaoChaControl : AsyncCtrl, IReturnListenerc
    {
        private JinShengService.JinShengServiceClient _client = new JinShengService.JinShengServiceClient();
        private JinShengService.JinShen_ChooseCandidator_Main chooseMain = null;
        private JinShengService.JinShen_MainInfo _mainInfo = null;
        private ObservableCollection<JinShengService.JinShen_KaoCha_Detail> details = new ObservableCollection<JinShengService.JinShen_KaoCha_Detail>();
        private ObservableCollection<JinShengService.JinShen_KaoCha_Detail> savedetails = new ObservableCollection<JinShen_KaoCha_Detail>();
       

        private bool CanEditInfo = true;
        private bool CanSubmit = true;
        private bool CanConfirm = false;

        public KaoChaControl(JinShengService.JinShen_MainInfo mainInfo)
        {
            InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;

            _mainInfo = mainInfo;
            details.Clear();
            TextBlock tb = new TextBlock() { Text = "科室:" + mainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
            selectionInfo.Children.Add(tb);
            tb = new TextBlock() { Text = "职位:" + mainInfo.Position + "(" + mainInfo.PositionLevel + ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
            selectionInfo.Children.Add(tb);

            #region
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                Role = e.Result;
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    if (_mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)  //人员考察
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }
                else
                {
                    if (_mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)
                    {
                        btnSubmit.IsEnabled = true;
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }
                

            };
            _client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);

            #endregion

            #region
         
            _client.GetJinShen_ChooseCandidator_MainCompleted += (sender, e) =>
            {
                chooseMain = e.Result.LastOrDefault();
                Init();
            };
            _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + mainInfo.ID);

            #endregion

            #region
            _client.UpdateJinShenMStatusCompleted += (sender, e) => { 
            
            
            };
            //消息
            _client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            _client.SaveJinShen_KaoCha_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _client.UpdateJinShenMStatusAsync(_mainInfo.MId, _mainInfo.Status);
                    SilverlightMessageBoxLibrary.Message.InfoMessage("上报成功");
                    imgBtn2.IsEnabled = true;

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                     sysinfo.MoKuaiSX = "党(工)委(组)考察";
                    sysinfo.DW1 = _mainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 2;
                    sysinfo.OrgIDs = _mainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
                }
            };

            _client.SaveJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _client.SaveJinShen_KaoCha_DetailAsync(details);
                }
            };
            #endregion
        }

     

        private ObservableCollection<JinShen_MainInfo> _mainInfos = null;

        private ObservableCollection<JinShen_MainInfo> SavemainInfos = null;

        private string Role = "";
        public KaoChaControl(ObservableCollection<JinShen_MainInfo> mainInfos)
        {
            InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;

            _mainInfos = mainInfos;
          
            gggg.Visibility = Visibility.Collapsed;

            #region
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                Role = e.Result;
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    if (mainInfos.First().Status == JinShenConst.STATUS_Candidate_Inspect)  //人员考察
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }
                else
                {
                    if (mainInfos.First().Status == JinShenConst.STATUS_Candidate_Inspect)
                    {
                        btnSubmit.IsEnabled = true;
                        if (imgBtn2.IsEnabled==false)
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }


            };
            _client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);

            #endregion

            JSControl2 js2 = null;
         
                foreach (var mainInfo in mainInfos)
                {
                   // details.Clear();
                    _mainInfo = mainInfo;
                    chooseMain = mainInfo.ChooseCandidator_Main.LastOrDefault();
                    if (_mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)//新增
                    {
                        if (mainInfo.KaoCha_Detail.Count != 0)
                        {
                            foreach (var item in mainInfo.KaoCha_Detail)
                            {
                                details.Add(item);
                            }
                            if (JinShenConst.Administrator.Contains(Role) && _mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)
                                imgBtn2.IsEnabled = false;
                            else imgBtn2.IsEnabled = true;
                        }
                        else
                        {
                            foreach (var d in chooseMain.Details)
                            {
                                JinShengService.JinShen_KaoCha_Detail detail = new JinShengService.JinShen_KaoCha_Detail()
                                {
                                    MainID = _mainInfo.ID,
                                    CreateDate = DateTime.Now,
                                    CreateUser = EHR.Common.Session.LoginUser.LoginName,
                                    Status = JinShenConst.STATUS_Candidate_Inspect,
                                    EmpID = d.EmpID,
                                    EmpName = d.Name,
                                };
                                details.Add(detail);
                            }
                        }
                    }
                    else if (_mainInfo.Status == JinShenConst.STATUS_Party_Meeting
               || _mainInfo.Status == JinShenConst.STATUS_Candidate_Show
               || _mainInfo.Status == JinShenConst.STATUS_Promot_Apply//编辑
               || _mainInfo.Status == JinShenConst.STATUS_Promot_Agree)//查看
                    {
                        foreach (var item in mainInfo.KaoCha_Detail)
                        {
                            details.Add(item);
                        }
                        imgBtn2.IsEnabled = true;
                    }
                 
                   
                    js2 = new JSControl2(mainInfo,details);
               
                    spDetails1.Children.Add(js2);
                }
                IsWorking.Visibility = Visibility.Collapsed;


            _client.SaveJinShen_KaoCha_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _client.SaveJinShenMainInfosStatusAsync(_mainInfos, JinShenConst.STATUS_Candidate_Inspect, _mainInfo.MId, EHR.Common.Session.LoginUser.LoginName);
             
                   // SilverlightMessageBoxLibrary.Message.InfoMessage("考察成功");
                    imgBtn2.IsEnabled = true;
                }
            };
            //消息
            _client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            _client.SaveJinShenMainInfosStatusCompleted += (sender, e) =>
            {

                if (e.Error == null)
                {
                    SavemainInfos = e.Result[0].JinShen_MainInfos;
                    SilverlightMessageBoxLibrary.Message.InfoMessage("考察成功");
                   // _client.SaveJinShen_KaoCha_DetailAsync(details);
                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "党(工)委(组)考察";
                    sysinfo.DW1 = _mainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 2;
                    sysinfo.OrgIDs = _mainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
                }
            };
           
        }


        public void BeginReturn()
        {
            return;
        }
        
        private void Init()
        {
       
                if (_mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)//新增
                {
                    _client.GetJinShen_KaoCha_DetailAsync("MainID=" + _mainInfo.ID);
                    _client.GetJinShen_KaoCha_DetailCompleted += (sender, e) =>
                    {
                        details = e.Result;
                       
                      //  LoadUI();
                        if (details.Count == 0)
                        {
                            foreach (var d in chooseMain.Details)
                            {
                                JinShengService.JinShen_KaoCha_Detail detail = new JinShengService.JinShen_KaoCha_Detail()
                                {
                                    MainID = _mainInfo.ID,
                                    CreateDate = DateTime.Now,
                                    CreateUser = EHR.Common.Session.LoginUser.LoginName,
                                    Status = JinShenConst.STATUS_Candidate_Inspect,
                                    EmpID = d.EmpID,
                                    EmpName = d.Name,
                                };
                                details.Add(detail);
                            }
                        }
                        else
                        {
                            if (JinShenConst.Administrator.Contains(Role)&& _mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)
                                imgBtn2.IsEnabled = false;
                            else imgBtn2.IsEnabled = true;
                        }

                        LoadUI();
                    };
                  
                  
                }
                else if (_mainInfo.Status == JinShenConst.STATUS_Party_Meeting
                    || _mainInfo.Status == JinShenConst.STATUS_Candidate_Show
                    || _mainInfo.Status == JinShenConst.STATUS_Promot_Apply//编辑
                    || _mainInfo.Status == JinShenConst.STATUS_Promot_Agree
                    || _mainInfo.Status == "退回"
                     || _mainInfo.Status == "终止办理"
                    )//查看
                {
                    _client.GetJinShen_KaoCha_DetailAsync("MainID=" + _mainInfo.ID);
                    _client.GetJinShen_KaoCha_DetailCompleted += (sender, e) =>
                    {
                        details = e.Result;
                        imgBtn2.IsEnabled = true;
                        LoadUI();
                    };
                 
                }

        }

        private void LoadUI()
        {
            spDetails.Children.Clear();
            foreach (var d in details)
            {
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                TextBlock tb = new TextBlock() { Text = d.EmpName, Width = 40, TextAlignment = TextAlignment.Right, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
                TextBox tBox = new TextBox() { MinHeight = 40, Width = 600, Margin = new Thickness(6, 0, 0, 0), TextWrapping = TextWrapping.Wrap,AcceptsReturn=true };
                tBox.IsReadOnly = !CanEditInfo;
                tBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("DSC") { Mode = System.Windows.Data.BindingMode.TwoWay });
                tBox.DataContext = d;
                sp.Children.Add(tb);
                sp.Children.Add(tBox);
                spDetails.Children.Add(sp);
            }
            IsWorking.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 考察情况上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                if (_mainInfos == null)
                {
                    _mainInfo.Status = JinShenConst.STATUS_Candidate_Inspect;
                    _client.SaveJinShenMainInfoAsync(_mainInfo);
                }
                else
                {
                    //var Status = JinShenConst.STATUS_Candidate_Inspect;
                    _client.SaveJinShen_KaoCha_DetailAsync(details);
                 }
            }
        }

        private bool Validate()
        {
            var detail = details.Where(c => string.IsNullOrEmpty(c.DSC)).FirstOrDefault();
            if (detail != null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请填写" + detail.EmpName + "考察信息");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 下一部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (_mainInfos == null)
            {
                JinShen_DangGongWeiControl control = new JinShen_DangGongWeiControl(_mainInfo) { Nav = this.Nav };
                Nav.ShowNextPage("党(工)委(组)考察", control as AsyncCtrl);
            }
            if (_mainInfos != null)
            {

            //    if (SavemainInfos == null) SavemainInfos = _mainInfos;
            //    JinShen_DangGongWeiControl control = new JinShen_DangGongWeiControl(SavemainInfos) { Nav = this.Nav };
            //Nav.ShowNextPage("党(工)委(组)考察", control as AsyncCtrl);

                _client.GetJinShen_MainInfosCompleted -= new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
                _client.GetJinShen_MainInfosCompleted += new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
                _client.GetJinShen_MainInfosAsync(string.Format("1=1 and jsm.ID={0}", _mainInfos.First().MId), Session.LoginUser.LoginName);
            }
        }
        void _client_GetJinShen_MainInfosCompleted(object sender, JinShengService.GetJinShen_MainInfosCompletedEventArgs e)
        {
          
            JinShen_DangGongWeiControl control = new JinShen_DangGongWeiControl(e.Result[0].JinShen_MainInfos) { Nav = this.Nav };
            Nav.ShowNextPage("党(工)委(组)考察", control as AsyncCtrl);
        }
    }
}
